package util;

import dao.FileContentDao;
import dao.FileInformationDao;
import model.FileContent;
import model.FileInformation;
import view.MainView;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * PackageName: util
 * ClassName: ExtractThread
 * Author: tum0r
 * Time: 2020/6/14 23:21
 */
public class ExtractThread implements Runnable {

    private MainView view;
    private final FileInformationDao fileInformationDao = new FileInformationDao();
    private final FileContentDao fileContentDao = new FileContentDao();
    private final FileOperator fileOperator = new FileOperator();
    private final String dirPath;
    private final String resourceDBPath;
    private String[] fileID;
    //    private String progress;
//    private Color progressColor;
    private int success = 0;
    private int fail = 0;

    public ExtractThread(String dirPath, String[] fileID, String resourceDBPath, MainView view) {
        this.dirPath = dirPath;
        this.fileID = fileID;
        this.resourceDBPath = resourceDBPath;
        this.view = view;
    }

    @Override
    public void run() {
        try {
            if (fileInformationDao.open(resourceDBPath)) {
                LinkedList<FileInformation> files = fileInformationDao.getFileInformationByID(fileID);
                fileInformationDao.close();
                if (fileContentDao.open(resourceDBPath)) {
                    success = 0;
                    int size = files.size();
                    for (int i = 0; i < size; i++) {
                        FileInformation fileInformation = files.get(i);
                        view.setProgress(null, "当前进度 %d/%d %.2f%%，正在提取%s，大小%.2f%s", (i + 1), size, (double) (i + 1) / size * 100, fileInformation.getName(), fileInformation.getSize(), fileInformation.getUnit());
                        String[] UUID = fileInformation.getContentID().split(" ");
                        LinkedList<FileContent> contents = fileContentDao.getFileContentByID(UUID);
                        boolean flag = true;
                        for (FileContent content : contents) {
                            String path = dirPath + File.separator + fileInformation.getName() + "." + fileInformation.getType();
                            flag &= fileOperator.writeFile(path, content.getData());
                            if (!flag) {
                                fileOperator.deleteFile(path);
                                break;
                            }
                        }
                        if (flag) {
                            success++;
                        } else {
                            fail++;
                        }
                    }
                    fileContentDao.close();
                    view.setProgress(null, "提取文件完成，成功提取%d个文件，提取失败%d个文件", success, fail);
                } else {
                    view.setProgress(Config.PROGRESS_LABEL_FONT_COLOR_ERROR, "数据库打开失败");
                }
            } else {
                view.setProgress(Config.PROGRESS_LABEL_FONT_COLOR_ERROR, "数据库打开失败");
            }
        } catch (ClassNotFoundException | SQLException | IOException classNotFoundException) {
            view.setProgress(Config.PROGRESS_LABEL_FONT_COLOR_ERROR, "数据库打开失败");
            classNotFoundException.printStackTrace();
        }
    }
}
